package jmind.core.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
 * @author weiboxie
 *
 */
public class RunnableTaskExecutor {

    private RunnableTaskExecutor() {
    }

    private static final RunnableTaskExecutor rte = new RunnableTaskExecutor();
    //Executors.newFixedThreadPool(nThreads)
    private static final ExecutorService executor = Executors.newCachedThreadPool();
    //  单线程  
    private static final ExecutorService simpleExe = Executors.newSingleThreadExecutor();

    public static RunnableTaskExecutor getInstance() {
        return rte;
    }

    public void runSimpleTask(Runnable task) {
        simpleExe.execute(task);
    }

    public void runTask(Runnable task) {
        executor.execute(task);
    }

    public void runTask(final Class<?> ownerClass, final String methodName, final Object... args) {
        Runnable task = new Runnable() {
            @Override
            public void run() {
                try {
                    MethodUtil.invokeMethod(ownerClass, methodName, args);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        executor.execute(task);

    }

}
